from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys#间接引用该库
from selenium.webdriver.common.action_chains import ActionChains
import time
import threading#线程库
'''
该脚本运用杨豪提供的网站本身bug，即双开网页会得到同一份试题，由此想到通过脚本双开测试网站，进而快速通过第一个网页获取答案，存在答案容器列表中
然后根据答案在第二个网页中选择选项即可！！！！
'''
answer_wrap=[]#初始化一个全局的答案容器
def main_for_answer():#该线程用来找答案
    #先点进第一个测试页面
    driver=webdriver.Chrome()
    target_url="http://2r5o535271.vicp.fun/logout/"#登录页面
    driver.get(target_url)
    stu_num=driver.find_element(By.NAME,"number")
    stu_password=driver.find_element(By.NAME,"password")
    submit=driver.find_element(By.XPATH,"/html/body/div[2]/div/div/form/input[3]")
    number="202308598"#学号
    password="Ss&1234567"#密码
    stu_num.send_keys(number)#输入学号
    stu_password.send_keys(password)#输入密码
    ActionChains(driver).click(submit).perform()#登录
    time.sleep(2)
    door=driver.find_element(By.XPATH,"/html/body/div[1]/div/ul/li[6]/a")
    ActionChains(driver).click(door).perform()
    time.sleep(2)
    #进入测试页面
    test_door=driver.find_element(By.XPATH,"/html/body/div[2]/div/div/table/tbody/tr/td[6]/span/a[1]")
    ActionChains(driver).click(test_door).perform()
    time.sleep(2)
    items=driver.find_elements(By.CLASS_NAME,"paper")#寻找题目
    nums=0
    for item in items:
        A=item.find_element(By.TAG_NAME,"input")
        ActionChains(driver).click(A).perform()
        if A.is_selected()==True:
            nums+=1

    if nums==50:
        print("已经全部选择！")
        # 写入“ok”
        checker = driver.find_element(By.XPATH, "/html/body/div[2]/div/div/form/div[51]/table/tbody/tr/td[2]/input")
        checker.send_keys("ok")
        time.sleep(2)
        # 交卷
        submit = driver.find_element(By.XPATH, "/html/body/div[2]/div/div/form/div[51]/table/tbody/tr/td[1]/input")
        ActionChains(driver).click(submit).perform()
        time.sleep(2)
        print("以下是正确答案：")
        answers = driver.find_elements(By.CLASS_NAME, "paper")
        for answer in answers:
            each_answer=answer.find_element(By.TAG_NAME,"span")
            each_answer=each_answer.text[-1]
            answer_wrap.append(each_answer)
        for i in range(0,49):
            print(answer_wrap[i])
    time.sleep(30)
def deputy_for_result():
    time.sleep(1)
    #该线程用来根据答案选择
    ###################
    # 先点进第一个测试页面
    driver = webdriver.Edge()
    target_url = "http://2r5o535271.vicp.fun/logout/"  # 登录页面
    driver.get(target_url)
    time.sleep(3)#避免几乎同时登录
    stu_num = driver.find_element(By.NAME, "number")
    stu_password = driver.find_element(By.NAME, "password")
    submit = driver.find_element(By.XPATH, "/html/body/div[2]/div/div/form/input[3]")
    number = "202308598"  # 学号
    password = "Ss&1234567"  # 密码
    stu_num.send_keys(number)  # 输入学号
    stu_password.send_keys(password)  # 输入密码
    ActionChains(driver).click(submit).perform()  # 登录
    time.sleep(2)
    door = driver.find_element(By.XPATH, "/html/body/div[1]/div/ul/li[6]/a")
    ActionChains(driver).click(door).perform()
    time.sleep(2)
    # 进入测试页面
    test_door = driver.find_element(By.XPATH, "/html/body/div[2]/div/div/table/tbody/tr/td[6]/span/a[1]")
    ActionChains(driver).click(test_door).perform()
    time.sleep(20)#强制等待另一个线程找答案
    items = driver.find_elements(By.CLASS_NAME, "paper")  # 寻找题目
    i=0
    for item in items:
        #寻找选项
        option=item.find_elements(By.TAG_NAME,"input")#返回一个长度为四或二的列表
        #根据答案选择
        if i<30:
            if answer_wrap[i] == "A":
                ActionChains(driver).click(option[0]).perform()
                print("选择a")
            elif answer_wrap[i] == "B":
                ActionChains(driver).click(option[1]).perform()
                print("选择b")
            elif answer_wrap[i] == "C":
                ActionChains(driver).click(option[2]).perform()
                print("选择c")
            elif answer_wrap[i] == "D":
                ActionChains(driver).click(option[3]).perform()
                print("选择d")
            else:
                print("选择失败!!!")
        else:
            # 判断题
            if answer_wrap[i] == "T":
                ActionChains(driver).click(option[0]).perform()
                print("选择T")
            elif answer_wrap[i] == "F":
                ActionChains(driver).click(option[1]).perform()
                print("选择F")
            else:
                print("选择失败！")
        i+=1
    #写入“ok”
    checker=driver.find_element(By.XPATH,"/html/body/div[2]/div/div/form/div[51]/table/tbody/tr/td[2]/input")
    checker.send_keys("ok")
    time.sleep(2)
    #交卷
    submit=driver.find_element(By.XPATH,"/html/body/div[2]/div/div/form/div[51]/table/tbody/tr/td[1]/input")
    ActionChains(driver).click(submit).perform()
    time.sleep(10)
if __name__=="__main__":
    first=threading.Thread(target=main_for_answer,name="first")
    second=threading.Thread(target=deputy_for_result, name="second")
    first.start()#先开始
    second.start()#后开始

